package com.yys.controller;


import com.alibaba.fastjson2.JSON;
import com.yys.entity.GetWarningSearch;
import com.yys.entity.Result;
import com.yys.entity.WarningTable;
import com.yys.service.WarningTableService;
import org.bytedeco.opencv.opencv_videostab.IFrameSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warningTable")
@CrossOrigin
public class WarningTableController {

    @Autowired
    private WarningTableService warningTableService;

    @GetMapping("/getwarning")
    public String searchWithSort() {
        List<WarningTable> list= warningTableService.searchWithSort();
        Result result = Result.success(list);
        if (list.size() > 0) {
            return JSON.toJSONString(Result.success("获取成功", list.size(), list));
        }
        return JSON.toJSONString(Result.success("获取失败", 0, list));
    }

    @GetMapping("/selectbytaskid")
    private String selectbytaskid(String alertId){
        WarningTable warningTable = warningTableService.searchByAlertId(alertId);
        if (warningTable != null){
            return JSON.toJSONString(Result.success("获取成功", 1, warningTable));
        }
        return JSON.toJSONString(Result.success("获取失败", 0, warningTable));
    }
    @PostMapping("/selectwarning")
    public String searchWarnings(@RequestBody GetWarningSearch getWarningSearch) {

        getWarningSearch.setPageNum(getWarningSearch.getPageNum()-1);

        Page<WarningTable> page = warningTableService.searchByAlertTypes(getWarningSearch);
        if (page.getContent().size() > 0) {
            return JSON.toJSONString(Result.success("获取成功", Math.toIntExact(page.getTotalElements()), page.getContent()));
        }
        return JSON.toJSONString(Result.success("获取失败", 0, page.getContent()));
    }

    @GetMapping("/getTodayTopAlertTypes")
    public String getTodayTopAlertTypes() {
        Map<String, Map<String, Long>> topAlertTypes = warningTableService.getTodayTopAlertTypes();
        return JSON.toJSONString(Result.success("获取成功", topAlertTypes.size(), topAlertTypes));
    }

    @GetMapping("/getSevenTopAlertTypes")
    public String getSevenTopAlertTypes() {
        Map<String, Map<String, Long>> topAlertTypes = warningTableService.getSevenTopAlertTypes();
        return JSON.toJSONString(Result.success("获取成功", topAlertTypes.size(), topAlertTypes));
    }

    @GetMapping("/getMonthTopAlertTypes")
    public String getTopAlertTypes() {
        Map<String, Map<String, Long>> topAlertTypes = warningTableService.getThreeDayTopAlertTypes();
        return JSON.toJSONString(Result.success("获取成功", topAlertTypes.size(), topAlertTypes));
    }



    @GetMapping("/test")
    public String test(){
        WarningTable warningTable=new WarningTable();
        warningTable.setAlertType("Smart");
        warningTable.setCameraPosition("camera001");
        warningTable.setAlertTime("2024-08-08 20:18:56");
        warningTable.setAlertLevel("high");
        warningTable.setMonitoringTask("task001");
        warningTable.setCapturedImage("http://192.168.71.25:9000/warningdata2024/img/08/2024-08-08/first_frame_0_1723119534.jpg");
        warningTable.setCapturedVideo("http://192.168.71.25:9000/warningdata2024/video/08/2024-08-08/output_0_1723119534.mp4");


        warningTable.setAlertId(warningTableService.generateCameraId());
        warningTableService.saveWarningTable(warningTable);
        return "333";
    }
}

